package com.brozers.islamdini;

import java.util.Calendar;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class MyXMLHandler extends DefaultHandler {

	Boolean currentElement = false;
	String month ;
	String currentValue = null;
	public static SitesList sitesList = null;
	String attr2;

	public static SitesList getSitesList() {
		return sitesList;
	}

	public static void setSitesList(SitesList sitesList) {
		MyXMLHandler.sitesList = sitesList;
	}

	/** Called when tag starts ( ex:- <name>AndroidPeople</name> 
	 * -- <name> )*/
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		 Calendar c =Calendar.getInstance();
		 int minute = c.get(Calendar.MINUTE);
		 int hour = c.get(Calendar.HOUR_OF_DAY);
		 String day = Integer.toString(c.get(Calendar.DAY_OF_MONTH));
		 month = Integer.toString(c.get(Calendar.MONTH)+1);

		currentElement = true;

		if (localName.equals("cityinfo"))
		{
			/** Start */ 
			sitesList = new SitesList();
		} else if (localName.equals("prayertimes")) {
			/** Get attribute value */
			attr2 = attributes.getValue("month");
			if(attr2.equals(month)){
			sitesList.setMonth(attr2);
			String attr = attributes.getValue("dayofyear");
			sitesList.setDayofyear(attr);
			String attr1 = attributes.getValue("day");
			sitesList.setDay(attr1);
			}
			
		}

	}

	/** Called when tag closing ( ex:- <name>AndroidPeople</name> 
	 * -- </name> )*/
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {

		currentElement = false;

		/** set value */ 
		if (localName.equalsIgnoreCase("prayertimes"))
			if(attr2.equals(month)){
			sitesList.setPrayertimes(currentValue);
			}
		

	}

	/** Called to get tag characters ( ex:- <name>AndroidPeople</name> 
	 * -- to get AndroidPeople Character ) */
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {

		if (currentElement) {
			currentValue = new String(ch, start, length);
			currentElement = false;
		}

	}

}
